<?php
/**
 * This is the template for generating CRUD search class of the specified model.
 */

use yii\helpers\StringHelper;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$modelClass = StringHelper::basename($generator->modelClass);
$serviceClass = StringHelper::basename($generator->serviceClass);
$formClass = StringHelper::basename($generator->formClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);


echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->serviceClass, '\\')) ?>;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') ?>;
use <?= ltrim($generator->formClass, '\\') ?>;

/**
 * <?= $serviceClass ?> represents the model behind the search form about `<?= $generator->modelClass ?>`.
 */
class <?= $serviceClass ?> extends \backend\components\BaseService
{
    public function initForm($scenario = 'create', $id = 0)
    {
        $model = new <?=$formClass ?>();
        if($scenario == 'create'){
            
        }elseif($scenario == 'update' && $id!==0){
            $data = $this->detail($id);
            $model->attributes = $data->attributes;
        }else{
            throw new \Exception("參數不正確。", 406);
        }
        return $model;
    }
    public function loadForm($params = [])
    {
        return !empty($params['<?=$formClass?>']) ? $params : false;
    }

    public function create($data, &$message = '')
    {
        $model = new <?=$modelClass ?>();
        return $this->save($model, $data, $message);
    }
    public function update($id, $data, &$message = '')
    {
        $model = $this->detail($id);
        if(empty($model)){
            throw new \Exception("$id 對應信息不存在", 406);
        }
        return $this->save($model, $data, $message);
    }
    public function save($model, $data, &$message = '')
    {
        if($model->load($data, '<?=$formClass?>') && $model->validate()){

            $transaction = Yii::$app->db->beginTransaction();
            try{
                $model->save();
                
                // 保存成功
                $transaction->commit();
                return true;               
                // end多語言擴展保存
            }catch(\Exception $e){
                $transaction->rollback();
                throw $e;
            }
            return false;
        }else{
            throw new \Exception(implode(",", $model->getErrors()) ,406);
        }
    }
    public function getList($params = [])
    {
        $query = <?=$modelClass ?>::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        return [
            'dataProvider' => $dataProvider
        ];
    }
    public function detail($id, $callBack=null){
        $model = <?=$modelClass ?>::find()->where(['id'=>$id]);
        if(gettype($callBack)=='object'){
            $model = $callBack($model);
        }
        return $model->one();
    }
    public function delete($id)
    {
        $model = $this->detail($id);
        if(empty($model)){
            throw new \Exception("$id 對應信息不存在", 406);
        }
        $model->delete();
        return true;
    }

}
